import {UseQueryOptions, useQuery, QueryKey} from "@tanstack/react-query";
import {ResponseData} from "@/types/utils";
import {Loading} from "@/components/k-view";

type QueryFn<TData extends Record<string, any>, TParams> = (params?: TParams) => Promise<ResponseData<TData>>;

export default function <D extends Record<string, any>, P>(queryFn: QueryFn<D, P>, params?: P, options?: any) {
    const queryKey: QueryKey = [queryFn.name, params];

    const query = useQuery<ResponseData<D>, Error>({
        queryKey: [...queryKey],
        queryFn: () => queryFn(params),
        ...options,
    });
    return {...query, Loading}
}

